package com.draven.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.annotation.Log;
import com.draven.common.Result;
import com.draven.entity.AccountPassword;
import com.draven.service.AccountPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 账号密码控制器
 */
@RestController
@RequestMapping("/account")
public class AccountPasswordController {

    @Autowired
    private AccountPasswordService accountPasswordService;

    /**
     * 添加账号密码
     */
    @PostMapping("/add")
    @Log(module = "账号密码", operationType = "ADD", description = "添加账号密码")
    public Result<Long> add(
            HttpServletRequest request,
            @NotBlank(message = "标题不能为空") @RequestParam String title,
            @NotBlank(message = "账号不能为空") @RequestParam String username,
            @NotBlank(message = "密码不能为空") @RequestParam String password,
            @RequestParam(required = false) String url,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String remark) {
        Long userId = (Long) request.getAttribute("userId");
        Long id = accountPasswordService.add(userId, title, username, password, url, category, remark);
        return Result.success(id);
    }

    /**
     * 修改账号密码
     */
    @PostMapping("/update")
    @Log(module = "账号密码", operationType = "UPDATE", description = "修改账号密码")
    public Result<Void> update(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id,
            @NotBlank(message = "标题不能为空") @RequestParam String title,
            @NotBlank(message = "账号不能为空") @RequestParam String username,
            @RequestParam(required = false) String password,
            @RequestParam(required = false) String url,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String remark) {
        Long userId = (Long) request.getAttribute("userId");
        accountPasswordService.update(id, userId, title, username, password, url, category, remark);
        return Result.success();
    }

    /**
     * 删除账号密码
     */
    @PostMapping("/delete")
    @Log(module = "账号密码", operationType = "DELETE", description = "删除账号密码")
    public Result<Void> delete(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        accountPasswordService.delete(id, userId);
        return Result.success();
    }

    /**
     * 获取账号密码详情
     */
    @GetMapping("/detail")
    public Result<AccountPassword> detail(
            HttpServletRequest request,
            @NotNull(message = "ID不能为空") @RequestParam Long id) {
        Long userId = (Long) request.getAttribute("userId");
        AccountPassword detail = accountPasswordService.getDetail(id, userId);
        return Result.success(detail);
    }

    /**
     * 分页查询账号密码
     */
    @GetMapping("/page")
    public Result<Page<AccountPassword>> page(
            HttpServletRequest request,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String category,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = (Long) request.getAttribute("userId");
        Page<AccountPassword> page = accountPasswordService.page(userId, title, username, category, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 生成随机密码
     */
    @GetMapping("/generatePassword")
    public Result<String> generatePassword(
            @RequestParam(defaultValue = "12") Integer length) {
        String password = accountPasswordService.generatePassword(length);
        return Result.success(password);
    }
} 